package redora.rdo.client.data;

import java.util.LinkedHashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONParser;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.types.TabBarControls;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

public class DataListCanvas extends Canvas {

	TabSet topTabSet = new TabSet();

	DynamicForm form = new DynamicForm();

	Tab findAll = new Tab("FindAll");

	ListGrid dataList = new ListGrid();

	VLayout mainPanel = new VLayout();

	DynamicForm langForm = new DynamicForm();

	public DataListCanvas(String objectName, DataSource dataSource) {
		// the right panel of tab to show
		topTabSet.setTabBarPosition(Side.TOP);
		topTabSet.setTabBarAlign(Side.LEFT);
		topTabSet.setWidth100();
		topTabSet.setHeight100();
		initTabs(topTabSet, objectName, dataSource);

		// the first tab for each model
		topTabSet.addTab(findAll, 0);
		LayoutSpacer layoutSpacer = new LayoutSpacer();
		layoutSpacer.setWidth(5);
		SelectItem item = new SelectItem("lang");
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("ch", "Chinese");
		map.put("en", "English");
		map.put("nl", "Dutch");
		item.setValueMap(map);
		item.addChangedHandler(new ChangedHandler() {

			public void onChanged(ChangedEvent event) {
				com.google.gwt.user.client.Window.Location.assign(GWT
						.getModuleBaseURL()
						+ "Application.html?locale="
						+ event.getItem().getValue());
			}

		});
		langForm.setPadding(0);
		langForm.setMargin(0);
		langForm.setCellPadding(1);
		langForm.setNumCols(1);
		langForm.setFields(item);
		topTabSet.setTabBarControls(TabBarControls.TAB_SCROLLER,
				TabBarControls.TAB_PICKER, layoutSpacer, langForm);
		topTabSet.addTabSelectedHandler(new TabSelectedHandler() {

			public void onTabSelected(TabSelectedEvent event) {
				DataVLayout vLayout = (DataVLayout) event.getTabPane();
				vLayout.destroy();
				vLayout.init();
				event.getTab().setPane(vLayout);
			}

		});

		DataVLayout dl = new DataVLayout(objectName, "", "", dataSource);
		dl.init();
		findAll.setPane(dl);

		this.setWidth100();
		this.setHeight100();
		this.addChild(topTabSet);

	}

	private void initTabs(final TabSet topTabSet, final String objectName,
			final DataSource dataSource) {
		String url = URL.encode(GWT.getModuleBaseURL()
				+ "modelFieldsView.do?action=getFindFields&objectName="
				+ objectName);

		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
		builder.setCallback(new RequestCallback() {
			public void onError(Request request, Throwable exception) {
				SC.say("getFindFields failed!");
			}

			public void onResponseReceived(Request request, Response response) {
				JSONArray jsonArray = JSONParser.parse(response.getText())
						.isArray();
				for (int i = 0; i < jsonArray.size(); i++) {
					Tab tab = new Tab(jsonArray.get(i).isObject().get(
							"finder_field_method").toString().replaceAll("\"",
							""));
					DataVLayout dl = new DataVLayout(objectName, jsonArray.get(
							i).isObject().get("finder_field_method").toString()
							.replaceAll("\"", ""), jsonArray.get(i).isObject()
							.get("finder_field_name").toString().replaceAll(
									"\"", ""), dataSource);
					dl.init();
					tab.setPane(dl);
					topTabSet.addTab(tab);
				}
			}
		});
		try {
			builder.send();
		} catch (RequestException e) {
			// e.printStackTrace();
		}
	}
}
